/*
 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import common from '@ohos.app.ability.common';
import window from '@ohos.window';
import { Log } from './Log';

const TAG = 'UIUtils';

export class UIUtils {
  static setStatusBarColor(context: common.Context, statusBarColor?: string) {
    window.getLastWindow(context, (error, window) => {
      if (window) {
        window.setWindowSystemBarEnable(['status'], (error) => {
          if (error) {
            Log.showError(TAG, 'error: ' + JSON.stringify(error));
          }
        });
        window.setWindowSystemBarProperties({
          statusBarColor: statusBarColor ?? '#00000000'
        });
        window.setWindowLayoutFullScreen(true, (error) => {
          if (error) {
            Log.showError(TAG, 'error: ' + JSON.stringify(error));
          }
        });
      }
    });
  }

  static getStatusBarHeight(context: common.Context) {
    window.getLastWindow(context, (error, topWindow) => {
      if (topWindow) {
        topWindow.getAvoidArea(window.AvoidAreaType.TYPE_SYSTEM, (error, avoidArea) => {
          if (avoidArea) {
            Log.showInfo(TAG, 'left: ' + JSON.stringify(avoidArea.leftRect));
            Log.showInfo(TAG, 'top: ' + JSON.stringify(avoidArea.topRect));
            Log.showInfo(TAG, 'right: ' + JSON.stringify(avoidArea.rightRect));
            Log.showInfo(TAG, 'bottom: ' + JSON.stringify(avoidArea.bottomRect));
            let statusBarHeight = px2vp(avoidArea.topRect.height);
            Log.showInfo(TAG, 'statusBarHeight: ' + statusBarHeight);
            AppStorage.SetOrCreate('statusBarHeight', statusBarHeight);
          }
        })
      }
    });
  }
}